package com.service.impl;

import com.mapper.CarouseMapper;
import com.pojo.Carouse;
import com.service.CarousService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class CarousServiceImpl implements CarousService {
    @Autowired
    private CarouseMapper carouseMapper;
    /**
     * 查询所有启用的轮播图
     */
    @Override
    public List<Carouse> selectActiveCarousels() {
        // 调用Mapper查询，可在此添加缓存逻辑或结果过滤
        return carouseMapper.selectActiveCarousels();
    }
    /**
     * 根据ID查询轮播图
     */
    @Override
    public Carouse selectById(Long id) {
        // 参数校验：避免无效ID查询
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("轮播图ID必须为正整数");
        }
        return carouseMapper.selectById(id);
    }
    /**
     * 新增轮播图
     */
    @Override
    public int insert(Carouse carouse) {
        // 参数校验：确保必要字段不为空
        if (carouse == null) {
            throw new IllegalArgumentException("轮播图对象不能为空");
        }
        if (carouse.getImgUrl() == null || carouse.getImgUrl().trim().isEmpty()) {
            throw new IllegalArgumentException("轮播图图片URL不能为空");
        }
        if (carouse.getSort() == null) {
            carouse.setSort(0);
        }
        if (carouse.getIsShow() == null) {
            carouse.setIsShow(1);
        }
        return carouseMapper.insert(carouse);
    }
    /**
     * 更新轮播图
     */
    @Override
    public int update(Carouse carouse) {
        // 参数校验：确保ID和必要字段有效
        if (carouse == null || carouse.getId() == null || carouse.getId() <= 0) {
            throw new IllegalArgumentException("轮播图ID必须为正整数");
        }
        if (carouse.getImgUrl() == null || carouse.getImgUrl().trim().isEmpty()) {
            throw new IllegalArgumentException("轮播图图片URL不能为空");
        }
        return carouseMapper.update(carouse);
    }
    /**
     * 根据ID删除轮播图
     */
    @Override
    public int deleteById(Long id) {
        // 参数校验：避免无效ID删除
        if (id == null || id <= 0) {
            throw new IllegalArgumentException("轮播图ID必须为正整数");
        }
        return carouseMapper.deleteById(id);
    }
}
